package vnetlab.ui;

import java.awt.Color;
import java.net.URL;
import java.text.NumberFormat;

import javax.swing.ImageIcon;
import javax.swing.UIManager;


import vnetlab.ui.editor.BasicGraphEditor;
import vnetlab.ui.editor.EditorMenuBar;
import vnetlab.ui.editor.EditorPalette;
import com.mxgraph.swing.mxGraphComponent;
import com.mxgraph.util.mxConstants;
import com.mxgraph.util.mxEvent;
import com.mxgraph.util.mxEventObject;
import com.mxgraph.util.mxEventSource.mxIEventListener;
import com.mxgraph.view.mxGraph;
import java.awt.BorderLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import javax.swing.JToolBar;
import vnetlab.ui.editor.EditorToolBar;

public class GraphEditor extends BasicGraphEditor {

    /**
     *
     */
    private static final long serialVersionUID = -4601740824088314699L;
    /**
     * Holds the shared number formatter.
     *
     * @see NumberFormat#getInstance()
     */
    public static final NumberFormat numberFormat = NumberFormat.getInstance();
    /**
     * Holds the URL for the icon to be used as a handle for creating new
     * connections. This is currently unused.
     */
    public static URL url = null;

    //GraphEditor.class.getResource("/vnetlab/ui/images/connector.gif");
    public GraphEditor() {
        this("VNetLab", new CustomGraphComponent(new CustomGraph()));
    }

    /**
     *
     */
    public GraphEditor(String appTitle, mxGraphComponent component) {
        super(appTitle, component);
        final mxGraph graph = graphComponent.getGraph();

        EditorPalette elementsPalette = insertPalette("Elements");


        elementsPalette.addListener(mxEvent.ADD, new mxIEventListener() {

            public void invoke(Object sender, mxEventObject evt) {
                System.out.println();
            }
        });

        elementsPalette.addListener(mxEvent.REMOVE_CELLS, new mxIEventListener() {

            public void invoke(Object sender, mxEventObject evt) {
                System.out.println();
            }
        });

        elementsPalette.addListener(mxEvent.CELL_CONNECTED, new mxIEventListener() {

            public void invoke(Object sender, mxEventObject evt) {
                System.out.println();
            }
        });

        elementsPalette.addListener(mxEvent.INSERT, new mxIEventListener() {

            public void invoke(Object sender, mxEventObject evt) {
                System.out.println();
            }
        });

        ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
        elementsPalette.addTemplate("Computer", new ImageIcon(GraphEditor.class.getResource("/vnetlab/ui/images/pc.png")),
                "image;image=/vnetlab/ui/images/pc.png", 60, 60, "Computer");

        elementsPalette.addTemplate("Switch", new ImageIcon(GraphEditor.class.getResource("/vnetlab/ui/images/switch.png")),
                "image;image=/vnetlab/ui/images/switch.png", 60, 60, "Switch");

        elementsPalette.addTemplate("Router", new ImageIcon(GraphEditor.class.getResource("/vnetlab/ui/images/router.png")),
                "image;image=/vnetlab/ui/images/router.png", 60, 60, "Router");

        elementsPalette.addTemplate("Server", new ImageIcon(GraphEditor.class.getResource("/vnetlab/ui/images/server.png")),
                "image;image=/vnetlab/ui/images/server.png", 60, 60, "Server");

        ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////


    }

    @Override
    protected void installToolBar() {
        add(new EditorToolBar(this, JToolBar.HORIZONTAL), BorderLayout.NORTH);
    }

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
        } catch (Exception e1) {
            e1.printStackTrace();
        }

        mxConstants.SHADOW_COLOR = Color.LIGHT_GRAY;
        GraphEditor editor = new GraphEditor();
        editor.createFrame(new EditorMenuBar(editor)).setVisible(true);
    }
}
